package guoanmaker.operator.business.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorFinancingServiceDownNew;
import guoanmaker.operator.business.model.OperatorFinancingServiceNew;
import guoanmaker.operator.business.model.OperatorFinancingServiceNewRound;
import guoanmaker.operator.business.model.PersonalFinanceProject;
import guoanmaker.operator.business.model.PersonalInvestorDisplay;
import guoanmaker.operator.business.modelnobind.PersonalFinanceProjectRo;
import guoanmaker.operator.business.repository.OperatorFinancingServiceDownNewRepository;
import guoanmaker.operator.business.repository.OperatorFinancingServiceNewRepository;
import guoanmaker.operator.business.repository.PersonalFinanceProjectRepository;
import guoanmaker.operator.business.repository.PersonalInvestorDisplayRepository;

/**
 * 
    * @ClassName: OperatorFinancingServiceNewService  
    * @Description: 投融资服务service层实现类  
    * @author lsy  
    * @date 2017年12月18日 下午3:41:33    
    *
 */
@Service
public class OperatorFinancingServiceNewService implements OperatorFinancingServiceNewServiceInterface {
	@Autowired
	private OperatorFinancingServiceNewRepository operatorFinancingServiceNewRepository;
	@Autowired
	private OperatorFinancingServiceDownNewRepository operatorFinancingServiceDownNewRepository;
	@Autowired
	private PersonalInvestorDisplayRepository personalInvestorDisplayRepository;
	@Autowired
	private PersonalFinanceProjectRepository  personalFinanceProjectRepository;
	/**
	 * 
	    * @Title: saveOperatorFinancingServiceNew  
	    * @Description: 保存投融资服务
	    * @param @param inRoundPhone 包装类
	    * @throws
	 */
	@Override
	@Transactional
	public void saveOperatorFinancingServiceNew(InRoundPhone inRoundPhone) {
		OperatorFinancingServiceNewRound operatorFinancingServiceNewRound = inRoundPhone.getOperatorFinancingServiceNewRound();
		OperatorFinancingServiceNew operatorFinancingServiceNew = operatorFinancingServiceNewRound.getOperatorFinancingServiceNew();
		operatorFinancingServiceNew.setCitycode(inRoundPhone.getCityId());
		operatorFinancingServiceNew = operatorFinancingServiceNewRepository.save(operatorFinancingServiceNew);
		List<OperatorFinancingServiceDownNew> operatorFinancingServiceDownNews = operatorFinancingServiceNewRound.getOperatorFinancingServiceDownNews();
		for (OperatorFinancingServiceDownNew FinancingServiceDownNew : operatorFinancingServiceDownNews) {
			FinancingServiceDownNew.setFinancialServiceId(operatorFinancingServiceNew.getId());
			operatorFinancingServiceDownNewRepository.save(FinancingServiceDownNew);
		}
	}
	
	/**
	 * 
	    * @Title: findAllOperatorFinancingServiceNew
	    * @Description: 根据cityId 投融资服务名称查询所有
	    * @param @param cityId 城市编码
	    * @param @param financingName 项目名称
	    * @return OperatorFinancingServiceNewRound   包装类
	    * @throws
	 */
	@Override
	public OperatorFinancingServiceNewRound findAllOperatorFinancingServiceNew(String cityId, String financingName) {
		OperatorFinancingServiceNewRound operatorFinancingServiceNewRound = new OperatorFinancingServiceNewRound();
		List<OperatorFinancingServiceNew> operatorFinancingServiceNews = operatorFinancingServiceNewRepository.findByCitycodeAndFinancingName(cityId, financingName);
		OperatorFinancingServiceNew operatorFinancingServiceNew = operatorFinancingServiceNews.get(0);
		operatorFinancingServiceNewRound.setOperatorFinancingServiceNew(operatorFinancingServiceNew);
		operatorFinancingServiceNewRound.setOperatorFinancingServiceDownNews(operatorFinancingServiceDownNewRepository.findByFinancialServiceId(operatorFinancingServiceNew.getId()));
		return operatorFinancingServiceNewRound;
	}
	/**
	 * 
	    * @Title: findInvestorDisplayById
	    * @Description: 根据金融大咖id查找
	    * @param @param financingId
	    * @return AjaxJson  
	    * @throws
	 */
	@Override
	public PersonalInvestorDisplay findInvestorDisplayById(String financingId) {
		return personalInvestorDisplayRepository.findOne(financingId);
	}
	
	/**
	 * 
	    * @Title: findFinanceProjectById
	    * @Description: 根据cityCode 跟金融名称 删除 金融服务 准备从新插入
	    * @param @param financingId
	    * @param @return   
	    * @return AjaxJson  
	    * @throws
	 */
	@Override
	public PersonalFinanceProjectRo findFinanceProjectById(String financingId) {
		PersonalFinanceProject financeProject = null;
		financeProject = personalFinanceProjectRepository.findByFinanceProjectNumberAndStatus(financingId, 0);//状态写死 0 是已通过
		PersonalFinanceProjectRo financeProjectRo = null;
		if (financeProject != null) {
			financeProjectRo = new PersonalFinanceProjectRo();
			BeanUtils.copyProperties(financeProject, financeProjectRo);
		}
	//	financeProject.setOperatorApprovalRecordList(null);
		return financeProjectRo;
	}
	/**
	 * 
	    * @Title: deleteByCityCodeAndFinancingName
	    * @Description: 根据cityCode 跟金融名称 删除 金融服务 准备从新插入
	    * @param @param financingId
	    * @param @return   
	    * @return AjaxJson  
	    * @throws
	 */
	@Override
	@Transactional
	public void deleteByCityCodeAndFinancingName(InRoundPhone inRoundPhone) {
		OperatorFinancingServiceNewRound operatorFinancingServiceNewRound = inRoundPhone.getOperatorFinancingServiceNewRound();
		OperatorFinancingServiceNew operatorFinancingServiceNew = operatorFinancingServiceNewRound.getOperatorFinancingServiceNew();
		List<OperatorFinancingServiceNew> operatorFinancingServiceNews = operatorFinancingServiceNewRepository.findByCitycodeAndFinancingName(inRoundPhone.getCityId(), operatorFinancingServiceNew.getFinancingName());
		for (OperatorFinancingServiceNew financingServiceNew : operatorFinancingServiceNews) {
			List<OperatorFinancingServiceDownNew> financingServiceDownNews = operatorFinancingServiceDownNewRepository.findByFinancialServiceId(financingServiceNew.getId());
			operatorFinancingServiceDownNewRepository.delete(financingServiceDownNews);
		}
		operatorFinancingServiceNewRepository.delete(operatorFinancingServiceNews);
		
	}

}
